import { ElLoading } from "element-plus";
import axios from "axios";

// import { getToken } from "/src/utils/common";

let loading

const startLoading = () => {
  const options = {
      lock: true,
      text: "请求中...",
      background: 'rgba(0, 0, 0, 0.7)'
  }
  loading = ElLoading.service(options)
}

const endLoading = () => {
  loading.close()
}

export function getBaseUrl() {
  let baseUrl = ""
  if (import.meta.env.MODE === "production") {
    baseUrl = "172.80.11.171:7000/api/"
  }
  else {
    // baseUrl = "127.0.0.1:8000/api/"   // 开发环境下根据不同的后端修改这部分地址即可
    baseUrl = "192.168.0.1:8000/api/"
  }
  return baseUrl
}

axios.defaults.baseURL = `http://${getBaseUrl()}`
axios.defaults.timeout = 20000

// 请求拦截
axios.interceptors.request.use((config) => {
  // config.headers["Authorization"] = getToken()
  return new Promise((resolve) => {
    resolve(config)
  });
}, (error) => {
  return Promise.reject(error);
}
)
// 响应拦截
axios.interceptors.response.use((response) => {
  return response.data;
}, (error) => {
  return Promise.reject(error);
})

export default axios;
